package com.ct.gd.passport.view.consult;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.Loader;
import android.view.View;
import android.widget.ListView;

import com.ct.gd.passport.common.constants.UmengVar;
import com.ct.gd.passport.domain.AdviceInfo;
import com.ct.gd.passport.view.AbsCatalogFragment;
import com.ct.gd.passport.view.BundleKeys;
import com.ct.gd.passport.view.CatalogListAdapter;
import com.ct.gd.passport.view.RootCatalogFragment.OnRootChangedListener;
import com.umeng.analytics.MobclickAgent;
import com.up91.common.android.content.SimpleLoader;
import com.up91.common.android.view.adapter.SimpleListAdapter;

public class ConsultCatalogFragment extends AbsCatalogFragment<AdviceInfo> implements OnRootChangedListener<ConsultType> {

	private int	mType;

	@Override
	public void onRootChanged(ConsultType root) {
		mType = root.getCode();
		loadCatalog();
	}

	@Override
	protected void beforeLoad(Loader<List<AdviceInfo>> loader) {
		ConsultCatalogLoader aLoader = (ConsultCatalogLoader) loader;
		aLoader.setType(mType);
	}

	@Override
	protected SimpleListAdapter<AdviceInfo> onInitListAdapter() {
		return new ConsultCatalogAdapter(getActivity(), null);
	}

	@Override
	public Loader<List<AdviceInfo>> onCreateLoader(int id, Bundle args) {
		return new ConsultCatalogLoader(getActivity(), getListView());
	}

	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
		AdviceInfo info = (AdviceInfo) l.getItemAtPosition(position);
		MobclickAgent.onEvent(getActivity(), UmengVar.ADVICE_MY_DETAIL);
		Intent intent = new Intent(this.getActivity(), ConsultDetailActivity.class);
		intent.putExtra(BundleKeys.ADVICE_ID, info.getId());
		startActivity(intent);
	}

	private static class ConsultCatalogAdapter extends CatalogListAdapter<AdviceInfo> {
		public ConsultCatalogAdapter(Context context, List<AdviceInfo> data) {
			super(context, data);
		}

		@Override
		protected String getDisp(AdviceInfo t) {
			return t.getTitle();
		}
	}

	private static class ConsultCatalogLoader extends SimpleLoader<List<AdviceInfo>> {
		private int	type;
		private int	pageSize	= 99;
		private int	pageIndex	= 0;

		public void setType(int type) {
			this.type = type;
		}

		public void setPageSize(int pageSize) {
			this.pageSize = pageSize;
		}

		public void setPageIndex(int pageIndex) {
			this.pageIndex = pageIndex;
		}

		public ConsultCatalogLoader(Context context, View view) {
			super(context, view);
		}

		@Override
		protected List<AdviceInfo> loadData() throws Exception {
			return AdviceInfo.load(type, pageIndex, pageSize);
		}
	}

}
